home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1997 August / Walnut Creek CDROM.7z / ZIPPED / LISTINGS / V_12_05.ZIP / ALLISON.ZIP / DATE.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-04  |  1.6 KB  |  86 lines

  1.  
  2. LISTING 3 -
  3. // date.cpp
  4.  
  5. #include <iostream.h>
  6. #include <assert.h>
  7. #include "time.h"
  8. #include "date.h"
  9.  
  10. // Must define static members at file scope:
  11. int Date::dtab[2][13] =
  12. {
  13.   {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
  14.   {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
  15. };
  16.  
  17. int Date::month(int m)
  18. {
  19.     // Change month
  20.     int old_month = month_;
  21.     if (m >= 1 && m <= 12)
  22.         month_ = m;
  23.     return old_month;
  24. }
  25.  
  26. int Date::day(int d)
  27. {
  28.     // Change day
  29.     int old_day = day_;
  30.     if (d >= 1 && d <= dtab[isleap(year_)][month_])
  31.         day_ = d;
  32.     return old_day;
  33. }
  34.  
  35. int Date::year(int y)
  36. {
  37.     // Change year
  38.     int old_year = year_;
  39.     year_ = y;
  40.     return old_year;
  41. }
  42.  
  43. int Date::compare(const Date& d1, const Date& d2)
  44. {
  45.     int months, days, years, order;
  46.  
  47.     years = d1.year_ - d2.year_;
  48.     months = d1.month_ - d2.month_;
  49.     days = d1.day_ - d2.day_;
  50.  
  51.     // return <0, 0, or >0, like strcmp()
  52.     if (years == 0 && months == 0 && days == 0)
  53.         return 0;
  54.     else if (years == 0 && months == 0)
  55.         return days;
  56.     else if (years == 0)
  57.         return months;
  58.     else
  59.         return years;
  60. }
  61.  
  62. ostream& operator<<(ostream& os, const Date& d)
  63. {
  64.     os << d.month_ << '/' << d.day_ << '/' << d.year_;
  65.     return os;
  66. }
  67.  
  68. istream& operator>>(istream& is, Date& d)
  69. {
  70.     char slash;
  71.     is >> d.month_ >> slash >> d.day_ >> slash >> d.year_;
  72.     return is;
  73. }
  74.  
  75. Date::Date()
  76. {
  77.     // Get today's date
  78.     time_t tval = ::time(0);
  79.     struct tm *tmp = ::localtime(&tval);
  80.  
  81.     month_ = tmp->tm_mon+1;
  82.     day_ = tmp->tm_mday;
  83.     year_ = tmp->tm_year + 1900;
  84. }
  85.  
  86.